package com.sove.face.controller;

import com.seeta.proxy.GenderPredictorProxy;
import com.seeta.proxy.MaskDetectorProxy;
import com.seeta.sdk.FaceAntiSpoofing;
import com.sove.face.service.SovefaceService;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

@Api(value = "soveface人脸识别API", protocols = "http")
@RestController
public class SovefaceController {

    @Autowired
    SovefaceService sovefaceService;

    @ApiOperation(value = "人脸检测", notes = "人脸检测")
    @PostMapping(value ="/faceDetector", consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
    public Boolean faceDetector(@RequestParam("file")MultipartFile faceImage) {
        Assert.notNull(faceImage, "上传人脸照片不能为空");
        return sovefaceService.faceDetector(faceImage);
    }

    @ApiOperation(value = "攻击人脸检测", notes = "攻击人脸检测")
    @PostMapping(value ="/faceAntiSpoofing", consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
    public ResponseEntity<List<FaceAntiSpoofing.Status>> faceAntiSpoofing(@RequestParam("file")MultipartFile faceImage) {

        Assert.notNull(faceImage, "上传人脸照片不能为空");
        List<FaceAntiSpoofing.Status> statuses = sovefaceService.faceAntiSpoofing(faceImage);
        return ResponseEntity.ok(statuses);
    }

    @ApiOperation(value = "识别人脸是否戴口罩", notes = "识别人脸是否戴口罩")
    @PostMapping("/maskDetector")
    public ResponseEntity<List<MaskDetectorProxy.MaskItem>> maskDetector(@RequestParam("file")MultipartFile faceImage) {
        Assert.notNull(faceImage, "上传人脸照片不能为空");
        List<MaskDetectorProxy.MaskItem> list = sovefaceService.maskDetector(faceImage);
        return ResponseEntity.ok(list);
    }

    @ApiOperation(value = "识别人脸照片性别", notes = "识别人脸照片性别")
    @PostMapping("/genderPredictor")
    public ResponseEntity<List<GenderPredictorProxy.GenderItem>> genderPredictor(@RequestParam("file")MultipartFile faceImage) {
        Assert.notNull(faceImage, "上传人脸照片不能为空");
        List<GenderPredictorProxy.GenderItem> list = sovefaceService.genderPredictor(faceImage);
        return ResponseEntity.ok(list);
    }

    @ApiOperation(value = "识别人脸照片年龄", notes = "识别人脸照片年龄")
    @PostMapping("/agePredictor")
    public ResponseEntity<List<Integer>> agePredictor(@RequestParam("file")MultipartFile faceImage) {
        Assert.notNull(faceImage, "上传人脸照片不能为空");
        List<Integer> ages = sovefaceService.agePredictor(faceImage);
        return ResponseEntity.ok(ages);
    }

    @ApiOperation(value = "识别人脸照片年龄", notes = "识别人脸照片年龄")
    @PostMapping("/faceRecognizer")
    public ResponseEntity<Float> faceRecognizer(@RequestParam("face1")MultipartFile face1, @RequestParam("face2")MultipartFile face2) {
        Assert.notNull(face1, "上传人脸照片1不能为空");
        Assert.notNull(face2, "上传人脸照片2不能为空");
        Float calculateSimilarity = sovefaceService.faceRecognizer(face1, face2);
        return ResponseEntity.ok(calculateSimilarity);
    }
}
